<HTML>
<!--
	THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
								J. Brook Monroe, mrprogguy@techie.com
    Copyright (c)1998 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

	Use at your own risk. No warranty is given or implied of the suitability 
	of this applet for any specific application. Neither Erica Sadun,
	J. Brook Monroe nor Charles River Media will be held responsible for any
	unwanted effects due to the use of this applet or any derivative. 
-->	
<HEAD>
	<TITLE>Joining an Array</TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript1.2"><!--
var myArray;

function Go(joinChar)
{
	var i;
	for(i = 0; i < 8; i++)
		myArray[i] = eval("document.forms[0].slot"+i+".value");
	var outStr = myArray.join(joinChar);
	eval('document.forms[0].result.value="'+outStr+'"');
}

myArray = new Array(8);
//-->
</SCRIPT>
<BODY>
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = LEFT>Joining an Array into a String</H1></FONT>
<BLOCKQUOTE><FONT COLOR="770000">
In this recipe we find out that JavaScript in Navigator can create strings from arrays.
</FONT><P>
(If you've seen the previous two recipes, this layout will not come as a surprise!)<BR>Fill in the top row of boxes and press the button representing the character that you'd like to have between the array elements.<BR>
<FORM>
The words:<BR>
<INPUT TYPE="text" VALUE="" NAME="slot0" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot1" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot2" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot3" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot4" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot5" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot6" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot7" SIZE=8><BR>
<INPUT TYPE="button" VALUE="Join with a space" onClick="Go(' ')">
<INPUT TYPE="button" VALUE="Join with a comma" onClick="Go(',')">
<INPUT TYPE="button" VALUE="Join with a hyphen" onClick="Go('-')">
<INPUT TYPE="button" VALUE="Join with a plus" onClick="Go('+')"><BR>
The Sentence:<BR>
<INPUT TYPE="text" VALUE="" NAME="result" SIZE=60>
</FORM>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
The JavaScript engine can combine array elements into an output string.  This can be useful when you've used the
<FONT COLOR="770000">Array.split()</FONT> function to turn a string into an array (probably to parse it), and need to get it put back together.
<FONT COLOR="770000">Array.join()</FONT> takes a single argument, which is the character you want separating the
array elements in the output string; if you omit the argument, the function joins the elements with commas.  If you 
use an empty string ('') as an argument, the function joins the elements with no separators of any sort between them.
</FONT>
<BR><BR><h5>Copyright ©1998 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>